package su.samlib.informer;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import su.samlib.informer.model.Author;
import su.samlib.informer.model.Book;
import su.samlib.informer.model.Library;
import su.samlib.informer.utils.updaters.BooksUpdater;
import su.samlib.informer.utils.updaters.samizdatupdaters.SamizdatApiBooksUpdater;
import su.samlib.informer.utils.updaters.samizdatupdaters.SamizdatPlainTextBooksUpdater;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AuthorListActivity extends Activity {

	private static final int DIALOG_DOWNLOAD_PROGRESS = 0;
	private ListView authorListView;
	private ProgressDialog mProgressDialog;
	AsyncTask<Author, Void, List<Book>> downloader;

	private OnItemClickListener onAuthorItemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			ListView list = (ListView) parent; 
			Author a = new Author();
			a.setAuthorUri((String) list.getItemAtPosition(position));
			downloader = new AuthorTextsDownloader().execute(a);
		}
	};

	@Override
	public void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setContentView(R.layout.author_list_activity);
		authorListView = (ListView) findViewById(R.id.AuthorList);

		String[] authorArray = getResources().getStringArray(
				R.array.default_author_list);
		ArrayAdapter<String> authorArrayAdaptor = new ArrayAdapter<String>(
				this, android.R.layout.simple_list_item_1, authorArray);
		authorListView.setAdapter(authorArrayAdaptor);
		authorListView.setOnItemClickListener(onAuthorItemClickListener);
	}

	private void onAuthorTextsDownloadFinished(Author a) {
		Intent authorTextsActivityIntent = new Intent(this,
				AuthorTextsActivity.class);

		authorTextsActivityIntent.putExtra("author", a);

		List<Book> texts = new ArrayList<Book>();
		try {
			texts.addAll(downloader.get());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		authorTextsActivityIntent.putParcelableArrayListExtra("authorTexts",
				(ArrayList<? extends Book>) texts);
		startActivity(authorTextsActivityIntent);
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_DOWNLOAD_PROGRESS:
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setTitle("Самиздат");
			mProgressDialog.setMessage("Загрузка произведений...");
			mProgressDialog.setOnCancelListener(new OnCancelListener() {

				public void onCancel(DialogInterface dialog) {
					// TODO Auto-generated method stub
					if (downloader != null) {
						if (downloader.getStatus() == AsyncTask.Status.RUNNING) {
							downloader.cancel(true);
						}
					}

				}
			});
			mProgressDialog.show();
			return mProgressDialog;
		default:
			return null;
		}
	}

	private class AuthorTextsDownloader extends
			AsyncTask<Author, Void, List<Book>> {
		private BooksUpdater updater;
		private Author a;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			updater = new SamizdatApiBooksUpdater();
			showDialog(DIALOG_DOWNLOAD_PROGRESS);
		}

		@Override
		protected List<Book> doInBackground(Author... a) {
			// TODO Auto-generated method stub
			this.a = a[0];
			updater.setAuthor(this.a);
			List<Book> texts = updater.getBooks();
			return texts;
		}

		@Override
		protected void onPostExecute(List<Book> result) {
			super.onPostExecute(result);
			dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
			onAuthorTextsDownloadFinished(a);
		}
	}
}
